#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>

#define PUBLIC_FIFO "MYFIFO"
#define PRIVATE1 "P_FO1"
#define PRIVATE1_HEADER "private 1 ->"

int main(int argc,char *argv[])
{
    int fd;
    int fd1;
    char num[100]={0};
    char buf[120]="";
    char buf1[120]=PRIVATE1_HEADER;
    pid_t pid;
    fd1=open(PRIVATE1,O_RDONLY);

    if(read(fd1,buf,120)!=-1)             //   read "hello client1"
    {
        printf("%s\n",buf);
        close(fd1);
    }

    while(1)
    {
        if((pid = fork()) == 0){
            if((fd=open(PUBLIC_FIFO,O_WRONLY))==-1)  //open the public 
            {
                printf("open failed\n");
                exit(1);
            }

            fgets(num,100,stdin);
            if(strncmp(num,"end",3)==0)
            {
                break;
            }
            strcpy(buf1,PRIVATE1_HEADER);
            strcat(buf1,num);
            if(write(fd,buf1,220)>0)
            {
                printf("write data success\n");
                close(fd);
            }
            usleep(200000);
        }
        else{
            fd1=open(PRIVATE1,O_RDONLY);
            if(read(fd1,buf,220)!=-1)
            {
                printf("%s\n\n",buf);
                close(fd1);
            }
            usleep(200000);
        }
        
    }
    //close(fd);
    return 0;

}